#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>

int main()
{
    //close(0);   // 关闭0
    //close(2);   // 关闭2
    //close(1);

    umask(0);
    //int fd = open("log.txt", O_WRONLY | O_CREAT | O_APPEND, 0666);
    int fd = open("log.txt", O_RDONLY);
    assert(fd != -1);

    dup2(fd, 0);    // 将fd的内容拷贝到0中
    
    char line[64];
    while(1)
    {
        printf("< ");
        // 读取结束退出while循环
        if(fgets(line, sizeof(line) - 1, stdin) == NULL)    
            break;
        printf("%s", line);
    }

    close(fd);

    return 0;


    //printf("open fd:%d\n", fd); // printf -> stdout
    //fprintf(stdout, "open fd:%d\n", fd);   // fprintf -> stdout 

    //const char* msg = "It's Crazy Thursday. Give me 50 yuan\n";
    //write(1, msg, strlen(msg));

    //fflush(stdout); // 刷新缓冲区

    //close(fd);

    //return 0;
}
